﻿using System;
using System.Collections.Generic;
using System.Text;
using TiaoTiaoCode.Common.Enums;

namespace TiaoTiaoCode.Common.Formatters
{
    public class TiaoTiaoByteTransFormatter
	{
		#region Public Properties

		public ByteDataFormatType DataFormat { get; set; }

		public bool IsStringReverseByteWord { get; set; }

        #endregion

        public TiaoTiaoByteTransFormatter()
        {
			DataFormat = ByteDataFormatType.DCBA;
			IsStringReverseByteWord = false;
		}

		#region DataFormat Support

		/// <summary>
		/// 反转多字节的数据信息
		/// </summary>
		/// <param name="value">数据字节</param>
		/// <param name="index">起始索引，默认值为0</param>
		/// <returns>实际字节信息</returns>
		public byte[] ByteTransDataFormat4(byte[] value, int index = 0)
		{
			byte[] buffer = new byte[4];
			switch (DataFormat)
			{
				case ByteDataFormatType.ABCD:
					{
						buffer[0] = value[index + 3];
						buffer[1] = value[index + 2];
						buffer[2] = value[index + 1];
						buffer[3] = value[index + 0];
						break;
					}
				case ByteDataFormatType.BADC:
					{
						buffer[0] = value[index + 2];
						buffer[1] = value[index + 3];
						buffer[2] = value[index + 0];
						buffer[3] = value[index + 1];
						break;
					}

				case ByteDataFormatType.CDAB:
					{
						buffer[0] = value[index + 1];
						buffer[1] = value[index + 0];
						buffer[2] = value[index + 3];
						buffer[3] = value[index + 2];
						break;
					}
				case ByteDataFormatType.DCBA:
					{
						buffer[0] = value[index + 0];
						buffer[1] = value[index + 1];
						buffer[2] = value[index + 2];
						buffer[3] = value[index + 3];
						break;
					}
			}
			return buffer;
		}


		/// <summary>
		/// 反转多字节的数据信息
		/// </summary>
		/// <param name="value">数据字节</param>
		/// <param name="index">起始索引，默认值为0</param>
		/// <returns>实际字节信息</returns>
		public byte[] ByteTransDataFormat8(byte[] value, int index = 0)
		{
			byte[] buffer = new byte[8];
			switch (DataFormat)
			{
				case ByteDataFormatType.ABCD:
					{
						buffer[0] = value[index + 7];
						buffer[1] = value[index + 6];
						buffer[2] = value[index + 5];
						buffer[3] = value[index + 4];
						buffer[4] = value[index + 3];
						buffer[5] = value[index + 2];
						buffer[6] = value[index + 1];
						buffer[7] = value[index + 0];
						break;
					}
				case ByteDataFormatType.BADC:
					{
						buffer[0] = value[index + 6];
						buffer[1] = value[index + 7];
						buffer[2] = value[index + 4];
						buffer[3] = value[index + 5];
						buffer[4] = value[index + 2];
						buffer[5] = value[index + 3];
						buffer[6] = value[index + 0];
						buffer[7] = value[index + 1];
						break;
					}

				case ByteDataFormatType.CDAB:
					{
						buffer[0] = value[index + 1];
						buffer[1] = value[index + 0];
						buffer[2] = value[index + 3];
						buffer[3] = value[index + 2];
						buffer[4] = value[index + 5];
						buffer[5] = value[index + 4];
						buffer[6] = value[index + 7];
						buffer[7] = value[index + 6];
						break;
					}
				case ByteDataFormatType.DCBA:
					{
						buffer[0] = value[index + 0];
						buffer[1] = value[index + 1];
						buffer[2] = value[index + 2];
						buffer[3] = value[index + 3];
						buffer[4] = value[index + 4];
						buffer[5] = value[index + 5];
						buffer[6] = value[index + 6];
						buffer[7] = value[index + 7];
						break;
					}
			}
			return buffer;
		}

		#endregion
	}
}
